import Jwt, { JsonWebTokenError, JwtPayload, TokenExpiredError } from "jsonwebtoken";
import config from "../config";

function sign(data: any){
    return Jwt.sign({data}, config.jwt.jwt_secret as string, { expiresIn: config.jwt.jwt_expire });
}

function verify(token: string): {admin: JwtPayload | string | null, error: TokenExpiredError | JsonWebTokenError | null}{
    try {
        let decoded = Jwt.verify(token, config.jwt.jwt_secret as string);
        return {
            admin: decoded,
            error: null
        }
    }catch(err){
        return {
            admin: null,
            error: err as TokenExpiredError | JsonWebTokenError | null
        }
    }
}

export {
    sign,
    verify 
}